using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace 水库上位机
{
    public class UCaterTan : Control
    {
        private string tankName = "水池";
        private double waterLevel = 0.5; // 0-1 表示水位比例
        private Color waterColor = Color.Blue;
        private bool isRoundCorner = true;
        private int cornerRadius = 8;

        [Category("外观")]
        [Description("水池名称")]
        public string TankName
        {
            get { return tankName; }
            set 
            {
                tankName = value;
                this.Invalidate();
            }
        }

        [Category("行为")]
        [Description("水位比例(0-1)")]
        public double WaterLevel
        {
            get { return waterLevel; }
            set 
            {
                waterLevel = Math.Max(0, Math.Min(1, value));
                this.Invalidate();
            }
        }

        [Category("外观")]
        [Description("水的颜色")]
        public Color WaterColor
        {
            get { return waterColor; }
            set 
            {
                waterColor = value;
                this.Invalidate();
            }
        }

        [Category("外观")]
        [Description("是否圆角")]
        public bool IsRoundCorner
        {
            get { return isRoundCorner; }
            set 
            {
                isRoundCorner = value;
                this.Invalidate();
            }
        }

        public UCaterTan()
        {
            this.DoubleBuffered = true;
            this.ResizeRedraw = true;
            this.Size = new Size(150, 150);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            int x = 10;
            int y = 30;
            int width = this.Width - 20;
            int height = this.Height - 40;

            // 绘制水池边框
            using (Pen pen = new Pen(Color.Black, 2))
            {
                if (isRoundCorner)
                {
                    GraphicsPath path = GetRoundRectangle(new Rectangle(x, y, width, height), cornerRadius);
                    g.DrawPath(pen, path);
                }
                else
                {
                    g.DrawRectangle(pen, x, y, width, height);
                }
            }

            // 绘制水
            int waterHeight = (int)(height * waterLevel);
            using (Brush brush = new SolidBrush(waterColor))
            {
                using (Brush transparentBrush = new SolidBrush(Color.FromArgb(150, waterColor)))
                {
                    if (isRoundCorner)
                    {
                        GraphicsPath path = GetRoundRectangle(new Rectangle(x, y + height - waterHeight, width, waterHeight), cornerRadius);
                        g.FillPath(brush, path);
                    }
                    else
                    {
                        g.FillRectangle(brush, x, y + height - waterHeight, width, waterHeight);
                    }
                }
            }

            // 绘制水池名称
            using (Font font = new Font("微软雅黑", 9F, FontStyle.Bold))
            {
                using (Brush brush = new SolidBrush(Color.Black))
                {
                    SizeF textSize = g.MeasureString(tankName, font);
                    g.DrawString(tankName, font, brush, (this.Width - textSize.Width) / 2, 5);
                }
            }
        }

        private GraphicsPath GetRoundRectangle(Rectangle rect, int radius)
        {
            GraphicsPath path = new GraphicsPath();
            int diameter = radius * 2;

            // 左上圆角
            path.AddArc(rect.X, rect.Y, diameter, diameter, 180, 90);
            // 右上圆角
            path.AddArc(rect.Right - diameter, rect.Y, diameter, diameter, 270, 90);
            // 右下圆角
            path.AddArc(rect.Right - diameter, rect.Bottom - diameter, diameter, diameter, 0, 90);
            // 左下圆角
            path.AddArc(rect.X, rect.Bottom - diameter, diameter, diameter, 90, 90);
            path.CloseFigure();

            return path;
        }
    }
}